﻿using HDSmartCommunity.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UpLoadController : ControllerBase
    {
        [Obsolete]
        private readonly IHostingEnvironment _hostingEnvironment;
        private JwtSettings _jwtSettings;
        JwtToken _jwttoken = new JwtToken();
        IHttpContextAccessor _access;

        [Obsolete]
        public UpLoadController(IHostingEnvironment hostingEnvironment, IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access)
        {
            _hostingEnvironment = hostingEnvironment;
            _jwtSettings = jwtSettingsAccesser.Value;

            _access = access;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpLoadFile")]
        [Obsolete]
        public ResponseMessage UpLoadFile([FromForm] IFormCollection formCollection)
        {
            ResponseMessage message = new ResponseMessage();
            string webRootPath = _hostingEnvironment.ContentRootPath;
            List<UploadFileEntity> uploadFileList = new List<UploadFileEntity>();
            FormFileCollection filelist = (FormFileCollection)formCollection.Files;
            try
            {
                foreach (IFormFile file in filelist)
                {
                    UploadFileEntity content = new UploadFileEntity();

                    string name = file.FileName;
                    string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    string FilePath = webRootPath + "/wwwroot/pfx/";

                    string type = System.IO.Path.GetExtension(name);
                    DirectoryInfo di = new DirectoryInfo(FilePath);


                    if (!di.Exists) { di.Create(); }

                    using (FileStream fs = System.IO.File.Create(FilePath + FileName + type))
                    {
                        // 复制文件
                        file.CopyTo(fs);
                        // 清空缓冲区数据
                        fs.Flush();
                        fs.Close();
                        //string hostip = GetLocalIp();
                        //int port = Request.HttpContext.Connection.LocalPort;
                        //string hostip = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
                        content.FileName = FileName + type;
                        content.FilePath = FilePath+ FileName + type;
                    }

                    uploadFileList.Add(content);
                }
                message.errcode = 0;
                message.errmsg = "";
                message.data = uploadFileList;
                message.total = 1;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "上传失败";
                message.data = null;
                message.total = 0;
            }

            return message;
        }
    }
}
